package com.middlegroup.payadmin.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.middlegroup.payadmin.mapper.RoleMapper;
import com.middlegroup.payadmin.mapper.RoleOperateMapper;
import com.middlegroup.payadmin.pojo.po.Role;
import com.middlegroup.payadmin.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @author pc
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper,Role> implements RoleService {
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private RoleOperateMapper roleOperateMapper;

    @Override
    public List<Role> listRole() {
        EntityWrapper<Role> ew = new EntityWrapper<>();
        ew.orderBy("id",true);
        return roleMapper.selectList(ew);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteRoleAndOperate(Integer[] ids) {
        List<Integer> sIds = Arrays.asList(ids);
        Lists.newArrayList(ids).forEach(id -> {
            roleMapper.deleteBatchIds(sIds);
            this.roleOperateMapper.deleteRoleOperateByRoleId(id);
        });
    }

}
